package net.trevize.galatee.main;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

import net.trevize.galatee.GEvent;
import net.trevize.galatee.GListener;
import net.trevize.galatee.Galatee;
import net.trevize.tinker.SystemCommandHandler2;

/**
 * 
 * 
 * @author Nicolas James <nicolas.james@gmail.com> [[http://njames.trevize.net]]
 * HTTPURLSuffixListExplorer.java - May 12, 2010
 */

public class HTTPURLSuffixListExplorer {

	private String host_prefix;
	private String path;

	public HTTPURLSuffixListExplorer(String host_prefix, String path) {
		this.host_prefix = host_prefix;
		this.path = path;
		init();
	}

	private void init() {
		// setting the system look and feel
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		System.out
				.println("*** HTTPURLSuffixListExplorer with Galatee main test ***");
		System.out.println("initializing...");

		int n = 3; //initial number of columns.
		System.out.println("path=[" + path + "]");
		System.out.println("display in " + n + " columns");

		//loading files and metadata.
		Vector<URI> v_uri = new Vector<URI>();
		Vector<Vector<Object>> v_object = new Vector<Vector<Object>>();

		try {
			FileReader fr = new FileReader(path);
			BufferedReader br = new BufferedReader(fr);

			String line;
			while ((line = br.readLine()) != null) {
				String relative_uri_value = line.split(",")[0];

				URI uri = new URI(host_prefix + "/" + relative_uri_value);
				v_uri.add(uri);
				//System.out.println(uri.toString());

				Vector<Object> v = new Vector<Object>();

				//get the image filename.
				String image_filename = line.substring(line.lastIndexOf("/"),
						line.length());

				v.add(image_filename);
				v_object.add(v);
			}

			br.close();
			fr.close();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		} catch (URISyntaxException e1) {
			e1.printStackTrace();
		}

		JFrame f = new JFrame("*** Galatee main test ***");

		JPanel p = new JPanel();
		p.setLayout(new BorderLayout());
		p.setBorder(new EmptyBorder(3, 3, 3, 3));
		f.getContentPane().add(p);

		Galatee g = new Galatee(v_uri, v_object, new Dimension(128, 128), 0, n);

		p.add(g);

		GListener listener = new GListener() {

			class SystemCommandThread extends Thread {
				private SystemCommandHandler2 sch2 = new SystemCommandHandler2();
				private String command;

				public SystemCommandThread(String command) {
					this.command = command;
				}

				public void run() {
					String[] commands = new String[1];
					commands[0] = command;
					sch2.exec(commands);
				}
			}

			@Override
			public void itemDoubleClicked(GEvent e) {
				System.out.println("item doubleclicked ["
						+ e.getSelectedItem().getLocalFilepath() + "]");
				SystemCommandThread sct = new SystemCommandThread("display \""
						+ e.getSelectedItem().getLocalFilepath() + "\"");
				sct.start();
			}

			@Override
			public void selectionChanged(GEvent e) {
				System.out.println("changing selection for ["
						+ e.getSelectedItem().getLocalFilepath() + "]");
			}

		};
		g.addGalateeListener(listener);

		g.enableSearchFunctionality();

		System.out.println("intialization ended.");

		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(512, 512);
		f.setLocationRelativeTo(null);
		f.setVisible(true);
	}

	public static void main(String args[]) {
		String host_prefix = "http://ronaldmasdom.dyndns.org/~nico/trecvid2005/data2005/devel";
		String url_suffix_list = "/home/nicolas/LSCOM/LSCOM_Annotation_v1.0/LSCOM_Annotation_v1.0/instance/000_Parade.txt";

		HTTPURLSuffixListExplorer explorer = new HTTPURLSuffixListExplorer(
				host_prefix, url_suffix_list);
	}

}
